package com.duoketech.utils.validator.rules.string;

import com.duoketech.exception.ValidateException;
import com.duoketech.utils.validator.AbstractRule;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringMustBeRex extends AbstractRule {
    private String rex;

    public StringMustBeRex(String v, String rex, String msg) {

        this.value = v;
        this.msg = msg;
        this.rex = rex;
    }

    @Override
    public boolean validate() throws ValidateException {
        String v = StringUtils.trim((String) this.value);
        Pattern pattern = Pattern.compile(this.rex);
        Matcher matcher = pattern.matcher(v);
        if (!matcher.matches()) {
            throw new ValidateException(this.msg);
        }
        return true;
    }
}
